package com.gzy.rpsm_admin.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.gzy.rpsm_admin.mapper.BuildingMapper;
import com.gzy.rpsm_admin.mapper.FloorMapper;
import com.gzy.rpsm_admin.mapper.RoomMapper;

import com.gzy.rpsm_admin.pojo.Floor;
import com.gzy.rpsm_admin.pojo.PageResult;
import com.gzy.rpsm_admin.pojo.Room;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RoomServiceImpl implements RoomService {


    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private BuildingMapper buildingMapper;
    @Autowired
    private FloorMapper floorMapper;


@Override
public PageResult<Room> pageOfRoom(Integer current, Integer size, String number) {
    PageHelper.startPage(current,size);//调用PageHelper分页方法
    Page<Room> rooms = roomMapper.findAllRoom(number);//调用roomMapper层查询符合条件的房间信息
    rooms.stream().forEach(room -> room.setFloor_number(floorMapper.queryFloorById(room.getRoom_floor_id())));
    //补充楼层信息：遍历查询结果，调用 floorMapper.queryFloorById 获取每个房间的楼层信息并设置
    rooms.stream().forEach(room -> room.setBuilding_name(buildingMapper.queryById(floorMapper.queryBuildingById(room.getRoom_floor_id()))));
    //补充楼栋信息：再次遍历查询结果，调用 floorMapper.queryBuildingById 获取楼层所属楼栋ID，再调用 buildingMapper.queryById 获取楼栋名称并设置。
    return PageResult.restPage(rooms);
    //返回结果：将分页结果封装为 PageResult<Room> 对象并返回
    }

}
